<html>
<head>
    <script src="/js/validate.js" type="text/javascript"></script>
    <script>
    common.setMainTitle('Thêm mới người dùng');
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body style="background-color: transparent">
<form id="id-form" name="frm" method="post" action="/admin/users/save">
    <input type=hidden name="id_u" value="<?=$this->id_u ?>" />
    <table>
        <tr>
            <td nowrap="nowrap">Tên đăng nhập <font color="red">*</font></td>
            <td>
                <input class="" id="iduser" name ="USERNAME" value="<?=(isset($this->data->USERNAME)?$this->data->USERNAME:'')?>" type ="text" size ="30" maxlength="50">
                <span style="color: red" id="ERRUSERNAME"></span>
            </td>
        </tr>
        <tr>
            <td nowrap="nowrap">Mật khẩu</td>
            <td><input class="" id="idpass" type ="password" name ="PASSWORD" type ="text" size ="30" maxlength="50">
                <span style="color: red" id="ERRPASSWORD"></span>
            </td>
        </tr>
        <tr>
            <td nowrap="nowrap">Họ và tên <font color="red">*</font></td>
            <td><input class="" id="idfullname" name ="FULLNAME" value="<?=(isset($this->data->FULLNAME)?$this->data->FULLNAME:'')?>" type ="text" size ="30" maxlength="50">
                <span style="color: red" id="ERRFULLNAME"></span>
            </td>
        </tr>
        <tr>
            <td nowrap="nowrap">Vai trò <font color="red">*</font></td>
            <td>
                <select name ="ROLE" style="width: 205px">
                    <option value="vt">-- Chọn vai trò --</option>
                    <?php
                    $selected = "";
                    foreach($this->listRole as $role){
                        if($role['CODE'] == $this->data->ROLE ){
                            $selected = "selected=''";
                        }else{
                            $selected = "";
                        }
                    ?>
                    <option <?=$selected?> value="<?=$role['CODE']?>"><?=$role['NAME']?></option>
                    <?php } ?>
                </select>
                <script>
                       document.frm.ROLE.value='<?=$this->data->ROLE?>';
                </script>
            </td>
        </tr>
        <tr>
            <td nowrap="nowrap">Địa chỉ Email</td>
            <td><input class="" id="idEmail" name ="EMAIL" value="<?=(isset($this->data->EMAIL)?$this->data->EMAIL:'')?>" type ="text" size ="30" maxlength="50">
                <span style="color: red" id="ERREMAIL"></span>
            </td>
        </tr>        
        <tr>
            <td nowrap="nowrap">Số CMND</td>
            <td><input class="" id="idCMND" name ="SOCMND" value="<?=(isset($this->data->SOCMND)?$this->data->SOCMND:'')?>" type ="text" size ="30" maxlength="50">
                <span style="color: red" id="ERRSOCMND"></span>
            </td>
        </tr>
        <tr>
            <td nowrap="nowrap">Số điện thoại</td>
            <td><input class="" id="idPhone" name ="PHONE" value="<?=(isset($this->data->PHONE)?$this->data->PHONE:'') ?>" type ="text" size ="30" maxlength="50">
                <span style="color: red" id="ERRPHONE"></span>
            </td>
        </tr>
        <tr>
            <td nowrap="nowrap">Công ty</td>
            <td>
                <select name ="COMPANY" style="width: 205px">
                    <option value="vt">-- Chọn công ty --</option>
                    <?php
                    $selected = "";
                    foreach($this->listCompany as $company){
                        if($company['id'] == $this->data->ID_COMPANY ){
                            $selected = "selected=''";
                        }else{
                            $selected = "";
                        }
                    ?>
                    <option <?=$selected?> value="<?=$company['id']?>"><?=$company['name']?></option>
                    <?php } ?>
                </select>
                <span style="color: red" id="ERRPHONE"></span>
            </td>
        </tr>
        <tr>
            <td nowrap="nowrap">Kích hoạt</td>
            <td>
                <input type="checkbox" name="STATUS" <?=(isset($this->data->STATUS) && $this->data->STATUS == "approved" ? "checked=''":''); ?> value="1" />
                <span style="color: red" id="ERRPHONE"></span>
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input class="form-submit" type="button" value="Lưu" onclick="save()">
         </td>
        </tr>        
    </table>
    <input type=hidden name="id" value="<?php echo $this->data->ID_U; ?>" />
</form>
<Script language="JavaScript">
   function validate() {
       var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
       var address = document.frm.EMAIL.value;
       if(reg.test(address) == false) {
          alert('Địa chỉ Email không hợp lệ!');
          return false;
       }
       return true;
    }

    function  save(){
        var err = true;
        err = err & validateInput("req",document.frm.USERNAME,"Tên đăng nhập không được rỗng");
        err = err & err==true?validateInput("maxlen=50",document.frm.USERNAME,"Tên đăng nhập không được quá 50 ký tự"):false;

        err = err & validateInput("req",document.frm.FULLNAME,"Họ tên không được rỗng");
        err = err & err==true?validateInput("maxlen=50",document.frm.FULLNAME,"Mật khẩu không được quá 50 ký tự"):false;

        err = err & validateInput("maxlen=50",document.frm.EMAIL,"Địa chỉ email không được quá 50 ký tự");
        err = err & validateInput("maxlen=20",document.frm.SOCMND," Số CMND không được quá 20 ký tự");
        err = err & validateInput("maxlen=50",document.frm.PHONE,"Số điện thoại không được quá 50 ký tự");
        if (err==true){
            document.frm.submit();
        }
    }

    function back(){
        document.location.href='/admin/users/index';
    }
</Script>
</body>
</html>